package com.tarena.demo.luban.all.main.controller;


import com.tarena.demo.luban.all.main.service.OrderService;
import com.tarena.demo.luban.commons.restful.JsonResult;
import com.tarena.demo.luban.protocol.order.param.OrderAddParam;
import com.tarena.luban.demo.stock.api.DubboTestApi;
import com.tarena.luban.demo.stock.api.DubboTestApiNew;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/order")
@Api(tags = "订单")
@Slf4j
public class OrderController {
    @Autowired(required = false)
    private DubboTestApi dubboTestApi;
    @Autowired(required = false)
    private DubboTestApiNew dubboTestApiNew;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @PostMapping("/add")
    @ApiOperation("新增订单的功能")
    public JsonResult addOrder(OrderAddParam orderAddParam){
        //先准备消息
        Message<OrderAddParam> message = MessageBuilder.withPayload(orderAddParam).build();
        rocketMQTemplate.asyncSend("order-add-topic", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("成了");
            }
            @Override
            public void onException(Throwable throwable) {
                //监控 报警系统 1 多长时间段 1年 10次失败 5次报警
                log.info("时间,节点,业务数据{订单编号,用户,商品,价格结算结果,...}");
            }
        });
        //TODO 和前端的交互逻辑 变化了
        return JsonResult.ok("接收到新增订单!");
    }
}
